package com.augurit.wf.app.document.dao;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.wf.app.document.entity.DocCategory;
import com.augurit.wf.app.document.utils.Version;


@Repository
@Transactional
public class DocCategoryDao extends BaseDao<DocCategory, Long> {

	
	public List<DocCategory> getChildrenCategory(Long id,String version){
		List<DocCategory> list = null;
		if(id != null && version != null ){
			String hql = "from DocCategory dc where dc.pid =? and dc.version =?";
			list = this.find(hql,id,version);
			return list;
		}
		return list;
		
		
	}
	
	public List<DocCategory> getCategoryUnderGivenCategory(Long pid, String childCatName){
		List<DocCategory> list = null;
		if(pid != null && childCatName != null ){
			String hql = "from DocCategory dc where dc.pid =? and dc.name =?";
			list = this.find(hql,pid,childCatName);
			return list;
		}
		return list;
		
		
	}
	
	//获取所有文档的版本
	public List<Version> getVersionList(){
		List<Version> list = new ArrayList<Version>();
		String hql = "select distinct(ps.version) from DocCategory ps";
		List<String> versionList = this.find(hql);
		if(versionList  !=null && versionList.size()>0){
			for(int i =0;i<versionList.size();i++){
				Version vs = new Version();
				vs.setId(Long.parseLong(i+""));
				String str = versionList.get(i);
				vs.setMainversion(str);
				list.add(vs);
			}
		}else{
			Version vs = new Version();
			vs.setId(1L);
			
			//获取系统年份
			Calendar calendar = Calendar.getInstance() ;
			Integer year = calendar.get(Calendar.YEAR);
			String str = year.toString();
			
			vs.setMainversion(str);
			list.add(vs);
		}
		return list;
	}
}
